package cn.famouscat.ex001;

import android.animation.AnimatorSet;
import android.animation.IntEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

    private static final String TAG = "MainActivity";

    Button mBtnA;
    Button mBtnB;
    Button mBtnC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnA = (Button) findViewById(R.id.btn_a);
        mBtnB = (Button) findViewById(R.id.btn_b);
        mBtnC = (Button) findViewById(R.id.btn_c);
    }


    public void onBtnClick(View v){
        switch (v.getId()){
            case R.id.btn_a:{
                animation();
            }break;
            case R.id.btn_b:{
                Intent intent = new Intent();
                intent.setClass(this,MyListActivity.class);
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
            }break;
            case R.id.btn_c:{
                animator();
            }break;
            case R.id.btn_d:{
                animatorSet();
            }break;
            case R.id.btn_e:{
                valueAnimator();
            }break;
        }
    }


    public void animation(){
        Animation a = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
        a.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.d(TAG,"=====AnimationListener:onAnimationStart"+ System.currentTimeMillis());
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.d(TAG,"=====AnimationListener:onAnimationEnd"+animation.getStartTime());
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

                Log.d(TAG,"=====AnimationListener:onAnimationRepeat"+animation.getStartTime());
            }
        });


        mBtnA.startAnimation(a);
    }


    public void animationDrawable(){

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void animator(){

        ObjectAnimator oa = ObjectAnimator.ofArgb(mBtnA,"backgroundColor",0xffff0000,0xff00ff00);
        oa.setDuration(3000);
        oa.start();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void animatorSet(){
        ObjectAnimator oa = ObjectAnimator.ofArgb(mBtnA,"backgroundColor",0xffff0000,0xff00ff00);
        ObjectAnimator ob = ObjectAnimator.ofArgb(mBtnA,"backgroundColor",0x00ff0000,0xff0000ff);
        ObjectAnimator oc = ObjectAnimator.ofArgb(mBtnB,"backgroundColor",0xff00ff00,0xff0000ff);
        ObjectAnimator od = ObjectAnimator.ofArgb(mBtnC,"backgroundColor",0xffff0000,0xff00ffff);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(
                oa,ob,oc,od
        );
        animatorSet.setDuration(3000);
        animatorSet.start();
    }


    public void valueAnimator(){
        final ValueAnimator va = ValueAnimator.ofInt(100);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            private IntEvaluator ie = new IntEvaluator();
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float fraction = animation.getAnimatedFraction();

                int value = (int) animation.getAnimatedValue();

                int valueE = ie.evaluate(fraction,1,100);

                Log.d(TAG,"=====value:"+value+";valueE:"+valueE);

            }
        });

        va.setDuration(3000);
        va.start();
    }





}
